/**
 * 
 * 起迪科技 Copyright (c) 2014-2018 QiDi,Inc.All Rights Reserved.
 */
package cn.qidisoft.edu.hzjt.service.impl;

import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.qidisoft.core.common.code.QdfRespCode;
import cn.qidisoft.core.common.exception.dao.DBException;
import cn.qidisoft.core.common.log.LoggerAdapter;
import cn.qidisoft.core.common.log.LoggerAdapterFactory;
import cn.qidisoft.core.common.pagination.Page;
import cn.qidisoft.core.common.pagination.PaginationBean;
import cn.qidisoft.edu.hzjt.dao.ProjectMaterialMapper;
import cn.qidisoft.edu.hzjt.model.ProjectMaterial;
import cn.qidisoft.edu.hzjt.service.IProjectMaterialService;
import cn.qidisoft.edu.hzjt.utils.LogType;

/**
 * 
 * 
 * @author Administrator
 * @version $Id: CatalogServiceImpl.java, v 0.1 2018年9月28日 上午9:17:27 Administrator hhl $
 */
@Service("projectMaterialService")
public class ProjectMaterialServiceImpl implements IProjectMaterialService {

    protected static final LoggerAdapter LOGGER = LoggerAdapterFactory
        .getLogger(LogType.HZJT_LOGGER.val);

    @Autowired
    private ProjectMaterialMapper        projectMaterialMapper;

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryList(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public List<ProjectMaterial> queryList(ProjectMaterial projectmaterial) {
        try {
            return projectMaterialMapper.queryList(projectmaterial);

        } catch (Exception e) {
            throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryByPK(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public ProjectMaterial queryByPK(ProjectMaterial projectmaterial) {
        try {
            return projectMaterialMapper.queryByPK(projectmaterial);
        } catch (Exception e) {
            throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#add(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void add(ProjectMaterial projectmaterial) {
        try {
            projectMaterialMapper.add(projectmaterial);
        } catch (Exception e) {
            throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#update(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void update(ProjectMaterial projectmaterial) {
        try {
            projectMaterialMapper.update(projectmaterial);
        } catch (Exception e) {
            throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#delete(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    @Transactional
    public void delete(ProjectMaterial projectmaterial) {
        try {
            projectMaterialMapper.delete(projectmaterial);
        } catch (Exception e) {
            throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#queryPageList(cn.qidisoft.microClass.resource.model.basis.Catalog, cn.qidisoft.core.common.pagination.Page)
     */
    @Override
    public PaginationBean<ProjectMaterial> queryPageList(ProjectMaterial projectmaterial,
                                                         Page page) {
        try {

            int c = projectMaterialMapper.queryPageListcount(projectmaterial);
            int totalRecords = 0;
            totalRecords = (int) c;

            PaginationBean<ProjectMaterial> pageHolder = new PaginationBean<ProjectMaterial>(page,
                totalRecords);

            List<ProjectMaterial> resultList = projectMaterialMapper.queryPageList(projectmaterial,
                new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                    pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /** 
     * @see cn.qidisoft.microClass.resource.service.ICatalogService#validation(cn.qidisoft.microClass.resource.model.basis.Catalog)
     */
    @Override
    public int validation(ProjectMaterial projectmaterial) {
        try {
            return projectMaterialMapper.validation(projectmaterial);
        } catch (Exception e) {
            e.printStackTrace();
            return -99;
        }
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.IProjectMaterialService#selectProjectByid(cn.qidisoft.edu.hzjt.model.ProjectMaterial)
     */
    @Override
    public List<ProjectMaterial> selectProjectByid(ProjectMaterial data1) {
        try {
            return projectMaterialMapper.selectProjectByid(data1);
        } catch (Exception e) {
            throw new DBException(QdfRespCode.QDF_020004.getCode(), QdfRespCode.QDF_020004.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.IProjectMaterialService#auditList(cn.qidisoft.edu.hzjt.model.ProjectMaterial)
     */
    @Override
    public void auditList(ProjectMaterial data) {
        try {
            for (String s : data.getIds()) {
                data.setProjectMaterialId(s);
                projectMaterialMapper.auditList(data);
            }
        } catch (Exception e) {
            LOGGER.error("update [{}] error," + this.getClass().getName() + "=[{}]",
                this.getClass().getName(),
                ToStringBuilder.reflectionToString(data, ToStringStyle.SHORT_PREFIX_STYLE));
            throw new DBException(QdfRespCode.QDF_020002.getCode(), QdfRespCode.QDF_020002.getMsg(),
                e);
        }
    }

    /** 
     * @see cn.qidisoft.edu.hzjt.service.IProjectMaterialService#projectmaterial_List(cn.qidisoft.edu.hzjt.model.ProjectMaterial, cn.qidisoft.core.common.pagination.Page)
     */
    @Override
    public PaginationBean<ProjectMaterial> projectmaterial_List(ProjectMaterial data, Page page) {
        try {

            int c = projectMaterialMapper.projectmaterial_Listcount(data);
            int totalRecords = 0;
            totalRecords = (int) c;

            PaginationBean<ProjectMaterial> pageHolder = new PaginationBean<ProjectMaterial>(page,
                totalRecords);

            List<ProjectMaterial> resultList = projectMaterialMapper.projectmaterial_List(data,
                new RowBounds((pageHolder.getCurrentPage() - 1) * pageHolder.getPageSize(),
                    pageHolder.getPageSize()));
            pageHolder.setPageList(resultList);
            return pageHolder;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
